﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entidades;
using UVM_Utileria;

namespace UVM_Pretira_Facade
{
    public class PataFac
    {
        public PataFac()
        {
        }

        public List<Pata> GetPata(Pata pat)
        {
            List<Pata> lstpata = null;
            Pata paux = null;
            DIAIMPARTIR diaux = null;
            using (PretiraDBDataContext db = new PretiraDBDataContext(Utilerias.Decriptar(Utilerias.ConnectionString)))
            {
                var ipat = from q in db.spGetPata(pat.Clave, pat.NRC)
                          select q;


                lstpata = new List<Pata>();
                if (ipat != null)
                {
                    foreach (var item in ipat)
                    {
                        diaux = new DIAIMPARTIR();
                        paux = lstpata.Find(delegate(Pata p) { return p.Clave == item.CLAVE && p.NRC == item.NRC; });
                        if (paux != null)
                        {
                            
                        }
                        else
                        {

                        }
                        lstpata.Add(paux);
                       

                    } 
                }
                
            }
            return lstpata;
        }


        public List<Pata> GetBuscarPata(string pat)
        {
            List<Pata> lstpata = null;

           String buscar = null;

           if (!string.IsNullOrEmpty(pat))
           {
               buscar = pat;
           }


           using (PretiraDBDataContext db = new PretiraDBDataContext(Utilerias.Decriptar(Utilerias.ConnectionString)))
            {
                var lp = from x in db.GetBuscarPata(buscar)
                         select new Pata()
                         {
                             NRC = x.NRC,
                             Clave = x.CLAVE,
                             Nombre = x.MATERIA,
                             Activa = x.Activa
                         };
                lstpata = new List<Pata>();
                if (lp!= null)
                {
                    foreach (var item in lp)
                    {
                        lstpata.Add(item);
                    }
                    //lstpata = lp.ToList<Pata>();
                }

            }

            return lstpata;
        }

        public Pata GetOnePata(Pata pat)
        {
            Pata patout = null;
            using (PretiraDBDataContext db = new PretiraDBDataContext(Utilerias.Decriptar(Utilerias.ConnectionString)))
            {
                var lspate = from x in db.GetPataLS(pat.Clave, pat.NRC)
                            select x;


                patout = new Pata();
                if (lspate != null)
                {
                    
                    DIAIMPARTIR impartir = null;
                    foreach (var item in lspate)
                    {
                        if (patout.LstDiasImparit == null)
                        {
                            patout.LstDiasImparit = new List<DIAIMPARTIR>();
                        }
                        impartir = new DIAIMPARTIR();

                        impartir.Dia = item.dias;
                        impartir.Horario = item.valor;
                        patout.LstDiasImparit.Add(impartir);
                        patout.Clave = item.CLAVE;
                        patout.Nombre = item.MATERIA;
                        patout.Tipo = item.CURSO;
                        patout.NRC = item.NRC;
                        patout.Salon = item.SALON;
                        patout.Activa = item.Activa;

                    }
                }

            }
            return patout;
        }

        public void UpdatePata(Pata p)
        {
            using (PretiraDBDataContext db = new PretiraDBDataContext(Utilerias.Decriptar(Utilerias.ConnectionString)))
            {
                db.SPUActiv(p.NRC, p.Clave, p.Activa);
            }
        }
    }
}
